package org.jz.gae.redbook.spb.server.controller;

import java.util.List;

import org.jz.gae.redbook.spb.server.helper.FamilyHelper;
import org.jz.gae.redbook.spb.server.helper.RedBookHelper;
import org.jz.gae.redbook.spb.shared.FamilyEntityView;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

public class FamilyControllerGoogle implements FamilyController
{

	@Override
	public void loadFamilyList(
			List<FamilyEntityView> _FamilyList)
	{
	    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    Key redbookKey = KeyFactory.createKey(RedBookHelper.RED_BOOK_SIGN, RedBookHelper.RED_BOOK_NAME);
	    Query query = new Query(FamilyHelper.ENTITY_NAME, redbookKey).addSort(FamilyHelper.FAMILY_ALIAS, Query.SortDirection.ASCENDING);
	    List<Entity> familys = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
	    if (familys.isEmpty()) 
	    {
	    	_FamilyList.clear();
	    } 
	    else 
	    {
	    	_FamilyList.clear();
	    	for (Entity family : familys)
	    	{
	    		FamilyEntityView newFamily = new FamilyEntityView();
	    		FamilyUtils.reflectEntityToView(family, newFamily);
	    		/**/
	    		_FamilyList.add(newFamily);
	    	}
	    }		
	}
	
	@Override
	public FamilyEntityView loadFamily(
			String _FamilyId) throws Exception 
	{
		System.err.println("loading key : " + _FamilyId);
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity family = datastore.get(KeyFactory.stringToKey(_FamilyId));
		System.err.println("loaded");
		FamilyEntityView result = new FamilyEntityView();
		FamilyUtils.reflectEntityToView(family, result);
		return result;
	}

	@Override
	public void updateFamily(
			FamilyEntityView _FamilyToEdit) throws Exception
	{
		System.err.println("extraction key : " + _FamilyToEdit.getFamilyKey());
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity family = datastore.get(KeyFactory.stringToKey(_FamilyToEdit.getFamilyKey()));
		System.err.println("extracted");
		FamilyUtils.reflectViewToEntity(_FamilyToEdit, family);
        datastore.put(family);
		System.err.println("updated");
	}

	@Override
	public void deleteFamily(
			FamilyEntityView _FamilyToDelete)
	{
		System.err.println("deletion key : " + _FamilyToDelete.getFamilyKey());
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.delete(KeyFactory.stringToKey(_FamilyToDelete.getFamilyKey()));
		System.err.println("deleted");
	}

	@Override
	public void createFamily(
			FamilyEntityView _FamilyToAppend)
	{
	    Key redbookKey = KeyFactory.createKey(RedBookHelper.RED_BOOK_SIGN, RedBookHelper.RED_BOOK_NAME);
		Entity newFamily = new Entity(FamilyHelper.ENTITY_NAME, redbookKey);
		FamilyUtils.reflectViewToEntity(_FamilyToAppend, newFamily);
        /**/
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(newFamily);
		System.err.println("new key : " + newFamily.getKey());
		_FamilyToAppend.setFamilyKey(KeyFactory.keyToString(newFamily.getKey()));
	}

}
